home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / OGC / reclaim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-06  |  4.7 KB  |  181 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1989, Tera Computer Company
  5.  **/
  6.  
  7. #include <stdio.h>
  8. #include "runtime.h"
  9. #define DEBUG
  10. #undef DEBUG
  11. #ifdef PRINTSTATS
  12. #  define GATHERSTATS
  13. #endif
  14.  
  15. long mem_found = 0;     /* Number of longwords of memory reclaimed     */
  16.  
  17. long composite_in_use;  /* Number of longwords in accessible composite */
  18.             /* objects.                                    */
  19.  
  20. long atomic_in_use;     /* Number of longwords in accessible atomic */
  21.             /* objects.                                 */
  22.  
  23. /*
  24.  * reclaim phase
  25.  *
  26.  */
  27.  
  28. reclaim()
  29. {
  30. register struct hblk *hbp;    /* ptr to current heap block        */
  31. register int word_no;        /* Number of word in block        */
  32. register long i;
  33. register word *p;        /* pointer to current word in block    */
  34. register int mb;        /* mark bit of current word        */
  35. int sz;                /* size of objects in current block    */
  36. word *plim;
  37. struct hblk **nexthbp;        /* ptr to ptr to current heap block    */
  38. int nonempty;            /* nonempty ^ done with block => block empty*/
  39. struct obj *list;        /* used to build list of free words in block*/
  40. register int is_atomic;         /* => current block contains atomic objs */
  41.  
  42. #   ifdef DEBUG
  43.         printf("clearing all between %x and %x, %x and %x\n",
  44.                objfreelist, &objfreelist[MAXOBJSZ+1],
  45.                aobjfreelist,&aobjfreelist[MAXAOBJSZ+1]);
  46. #   endif
  47.     { register struct obj **fop;
  48.     for( fop = objfreelist; fop < &objfreelist[MAXOBJSZ+1]; fop++ ) {
  49.         *fop = (struct obj *)0;
  50.     }
  51.     for( fop = aobjfreelist; fop < &aobjfreelist[MAXAOBJSZ+1]; fop++ ) {
  52.         *fop = (struct obj *)0;
  53.     }
  54.     }
  55.     
  56.     atomic_in_use = 0;
  57.     composite_in_use = 0;
  58.  
  59. #   ifdef PRINTBLOCKS
  60.         printf("reclaim: current block sizes:\n");
  61. #   endif
  62.  
  63.   /* go through all heap blocks (in hblklist) and reclaim unmarked objects */
  64.     nexthbp = hblklist;
  65.  
  66.     while( nexthbp < last_hblk ) {
  67.     hbp = *nexthbp++;
  68.  
  69.     nonempty = FALSE;
  70.     sz = hbp -> hb_sz;
  71.     is_atomic = 0;
  72.     if (sz < 0) {
  73.         sz = -sz;
  74.         is_atomic = 1;        /* this block contains atomic objs */
  75.     }
  76. #    ifdef PRINTBLOCKS
  77.             printf("%d(%c",sz, (is_atomic)? 'a' : 'c');
  78. #    endif
  79.  
  80.     if( sz > (is_atomic? MAXAOBJSZ : MAXOBJSZ) ) {  /* 1 big object */
  81.         mb = mark_bit(hbp, (hbp -> hb_body) - ((word *)(hbp)));
  82.         if( mb ) {
  83. #               ifdef GATHERSTATS
  84.             if (is_atomic) {
  85.             atomic_in_use += sz;
  86.             } else {
  87.             composite_in_use += sz;
  88.             }
  89. #               endif
  90.         nonempty = TRUE;
  91.         } else {
  92.         mem_found += sz;
  93.         }
  94.     } else {                /* group of smaller objects */
  95.         p = (word *)(hbp->hb_body);
  96.         word_no = ((word *)p) - ((word *)hbp);
  97.         plim = (word *)((((unsigned)hbp) + HBLKSIZE)
  98.                - WORDS_TO_BYTES(sz));
  99.  
  100.         list = (is_atomic) ? aobjfreelist[sz] : objfreelist[sz];
  101.  
  102.       /* go through all words in block */
  103.         while( p <= plim )  {
  104.         mb = mark_bit(hbp, word_no);
  105.  
  106.         if( mb ) {
  107. #                   ifdef GATHERSTATS
  108.             if (is_atomic) atomic_in_use += sz;
  109.             else           composite_in_use += sz;
  110. #                   endif
  111. #                   ifdef DEBUG
  112.                         printf("found a reachable obj\n");
  113. #            endif
  114.             nonempty = TRUE;
  115.             p += sz;
  116.         } else {
  117.           mem_found += sz;
  118.           /* word is available - put on list */
  119.             ((struct obj *)p)->obj_link = list;
  120.             list = ((struct obj *)p);
  121.           if (is_atomic) {
  122.             p += sz;
  123.           } else {
  124.             /* Clear object, advance p to next object in the process */
  125.             i = (long)(p + sz);
  126.                         p++; /* Skip link field */
  127.                         while (p < (word *)i) {
  128.                 *p++ = 0;
  129.             }
  130.           }
  131.         }
  132.         word_no += sz;
  133.         }
  134.  
  135.       /*
  136.        * if block has reachable words in it, we can't reclaim the
  137.        * whole thing so put list of free words in block back on
  138.        * free list for this size.
  139.        */
  140.         if( nonempty ) {
  141.         if ( is_atomic )    aobjfreelist[sz] = list;
  142.         else            objfreelist[sz] = list;
  143.         }
  144.     } 
  145.  
  146. #    ifdef PRINTBLOCKS
  147.             printf("%c),", nonempty ? 'n' : 'e' );
  148. #    endif
  149.     if (!nonempty) {
  150.             if (!is_atomic && sz <= MAXOBJSZ) {
  151.                 /* Clear words at beginning of objects */
  152.                 /* Since most of it is already cleared */
  153.           p = (word *)(hbp->hb_body);
  154.           plim = (word *)((((unsigned)hbp) + HBLKSIZE)
  155.              - WORDS_TO_BYTES(sz));
  156.           while (p <= plim) {
  157.             *p = 0;
  158.             p += sz;
  159.           }
  160.         hbp -> hb_uninit = 0;
  161.         } else {
  162.         /* Mark it as being uninitialized */
  163.         hbp -> hb_uninit = 1;
  164.         }
  165.  
  166.       /* remove this block from list of active blocks */
  167.         del_hblklist(hbp);    
  168.  
  169.         /* This entry in hblklist just got replaced; look at it again  */
  170.         /* This admittedly depends on the internals of del_hblklist... */
  171.         nexthbp--;
  172.  
  173.         freehblk(hbp);
  174.     }  /* end if (one big object...) */
  175.     } /* end while (nexthbp ...) */
  176.  
  177. #   ifdef PRINTBLOCKS
  178.         printf("\n");
  179. #   endif
  180. }
  181.